<?php

class main extends P_Controller {

	var $import_message;
	
	function __construct()
	{
		parent::__construct();
		$this->load->model("user");
	}
	
	function testemail()
	{
		 $this->load->library('email');
		 $this->email->from('satiri.a@gmail.com','Team OnePage');
		 $this->email->to("satiri.a@gmail.com");
		 $this->email->subject('A test email from CodeIgniter using Gmail');
		 $this->email->message("I can now email from CodeIgniter using Gmail as my server!");
		 $this->email->send(); 
	}
	
	function chgpwd($id=0)
	{
		$data = array();
		switch($id){		
			case 1:
				$data["msg"] = "Gagal, Password lama tidak sama atau password baru <> konfirmasi";
			break;
			
			case 2:
				$data["msg"] = "Sukses ganti password";
			break;
			
			default:
				$data["msg"] = "";
			break;
		}
		$this->load->view("default/gantipassword",$data);
	}
		
	function update_password()
	{
		$passwordlama = $this->input->post("passwordlama");
		$passwordbaru = $this->input->post("passwordbaru");
		$konfirmasi = $this->input->post("konfirmasi");
		
		if($passwordlama==$this->session->userdata("password") && ($konfirmasi==$passwordbaru))
		{
			$this->user->update_password($this->session->userdata("user_id"),$passwordbaru);
			redirect("main/chgpwd/2");
		}
		else
		{
			redirect("main/chgpwd/1");
		}
	}
	
	function index($failed=0,$versi="default")
	{
		
		$this->_set_view_dir($versi);
		
		if($this->session->userdata('logged_in')==true && strlen($this->session->userdata('username'))>0)
		{
			//redirect to home
			redirect("main/home",null);
		}
		else
		{
			$data["message"] = "";
			if($failed==1)
			{
				$data["message"]="Login Failed. Please notice that password is case sensitive.";
			}
			
			$data['nama_perusahaan'] = $this->konfigurasi->get_nama_perusahaan();
			$data['logo'] = $this->konfigurasi->get_logo();
 			$this->_load_view("login",$data);
		}
	}
	
	function login()
	{
		$exist = $this->user->is_exists($this->input->post("email"),$this->input->post("password"));
		if($exist)
		{
			$data_user = $this->user->get_user($this->input->post("email"),$this->input->post("password"));
			$newdata = array(
							   'email'  => $this->input->post("email"),
							   'logged_in' => TRUE,
							   'id_user' => $data_user->id_user,
							   'password' =>$this->input->post("password"),
							   'level' => 0,
							   'id_role'=>$data_user->id_role
						   );

			$this->session->set_userdata($newdata);		
			redirect("user",null);
		}
		else
		{
			redirect("main/index/1",null);
		}
	}
	
	/**
	* Dev Mode Function
	* to enable database access without having to do phpmyadmin	
	*/	
	function run_query()
	{
		if($this->config->item("development_mode")==TRUE)
		{
			$sql=$this->input->post("sql");
			echo $sql."<br>";
			$data["action"] = site_url()."/main/run_query";
			$this->load->view("default/sqlcmd",$data);
			
			if(strlen($sql)>0)
			{
				$rec = $this->db->query($sql);
				if($rec->num_rows()>0)
				{
					echo "<pre>";
					print_r($rec->result());
					echo "</pre>";
				}
			}
			
		}		
	}
	
	function noentry()
	{
		$data["msg"] = "Maaf, User yang anda gunakan tidak memiliki hak untuk melakukan penambahan/perubahan atas data. <br> Hubungi Administrator jika ini suatu kesalahan.";
		$this->load->view("default/noentry",$data);
	}
	
	
	function logout($nextpage=null)
	{
		$newdata = array(
						   'email'  => "",
						   'logged_in' => FALSE
					   );

		$this->session->set_userdata($newdata);			
		
		if($nextpage==null)
		{
			redirect("main/index/0",null);
		}
		else
		{
			$nextpage = str_replace("~","/",$nextpage);
			redirect($nextpage,null);
		}
	}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */